import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;

public class TopicPieChart {
	List<PieChart.Data> chartData;
	PieChart chart;
	final JFXPanel fxPanel;
	final ObservableList<PieChart.Data> pieChartData;
	Map<String, Integer> promoMap;

	public JFXPanel getPieChart() {
		return fxPanel;
	}

	public TopicPieChart(Map<String, ArrayList<String>> tweetMap,
			final Map<String, Integer> promoMap) {
		this.promoMap = promoMap;
		Set<String> keyList = tweetMap.keySet();
		chartData = new ArrayList<PieChart.Data>();
		for (String key : keyList) {
			chartData.add(new PieChart.Data(key, tweetMap.get(key).size()));

		}

		pieChartData = FXCollections.observableArrayList(chartData);

		chart = new PieChart(pieChartData);
		chart.setMaxSize(425, 480);
		chart.setTitle("State Farm Topics");
		fxPanel = new JFXPanel();

		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				Group root = new Group();
				Scene scene = new Scene(root, 425, 480);
				root.getChildren().add(chart);
				fxPanel.setScene(scene);
			}
		});

	}

	public Node getPromoNode() {
		for (final PieChart.Data data : chart.getData()) {
			if (data.getName().equals("Promotions")) {
				return data.getNode();
			}
		}
		return null;
	}

}
